package xyz.ranyh.jobclient.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import xyz.ranyh.jobclient.AboutActivity;
import xyz.ranyh.jobclient.FindPasswordActivity;
import xyz.ranyh.jobclient.LoginActivity;
import xyz.ranyh.jobclient.ModifyPasswdActivity;
import xyz.ranyh.jobclient.R;
import xyz.ranyh.jobclient.SettingsActivity;
import xyz.ranyh.jobclient.http.Resource;
import xyz.ranyh.jobclient.util.Setting;
import xyz.ranyh.jobclient.util.User;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link UserFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link UserFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class UserFragment extends Fragment implements AdapterView.OnItemClickListener {
    private TextView mNicknameView;
    private TextView mEmailView;
    private TextView mStarCountView;
    private TextView mAlarmCountView;
    private Button mLogoutButton;

    private ListView mActionList;


    private OnFragmentInteractionListener mListener;

    public UserFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment UserFragment.
     */
    public static UserFragment newInstance(String param1, String param2) {
        UserFragment fragment = new UserFragment();
        return fragment;
    }

    public void updateData() {
        User user = Resource.getInstance().getUser();
        Log.d("current user", mNicknameView.toString());
        mNicknameView.setText(user.getNickname());
        mEmailView.setText(user.getEmail());
        mStarCountView.setText("我的关注("+String.valueOf(user.getStar().size()) + ")");
        mAlarmCountView.setText("我的提醒("+String.valueOf(user.getAlarm().size()) + ")");
    }

    private List<String> actions() {
        List<String> list = new ArrayList<String>();

        list.add("修改密码");
        list.add("我的设置");
        list.add("关于软件");

        return list;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.user, container, false);
        mNicknameView = (TextView) view.findViewById(R.id.nickname_view);
        mEmailView = (TextView) view.findViewById(R.id.email_view);
        mActionList = (ListView) view.findViewById(R.id.user_action_list);
        mLogoutButton = (Button) view.findViewById(R.id.logout_button);
        mStarCountView = (TextView) view.findViewById(R.id.star_count_view);
        mAlarmCountView = (TextView) view.findViewById(R.id.alarm_count_view);

        mActionList.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, actions()));

        mLogoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Resource.getInstance().logout();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        mActionList.setOnItemClickListener(this);

        updateData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                startActivity(new Intent(getActivity(), ModifyPasswdActivity.class));
                break;
            case 1:
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                break;
            case 2:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}
